﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace AutoDeployForSims.From18To10Cfg.Utility
{
    //10
    //电源屏,    0
    //外电网,    1
    //UPS,    2
    //轨道电压,   3
    //轨道电流,   4
    //轨道相位角,  5
    //高压不对称电压,    6
    //牵引电流干扰电压,   7
    //计轴, 8
    //电码化发送电压,    9
    //电码化发送电流,    10
    //电码化载频,  11
    //电码化低频,  12
    //电码化上边频, 13
    //电码化下边频, 14
    //移频发送器发送电压,  15
    //移频发送器发送电流,  16
    //移频发送器载频,    17
    //移频发送器低频,    18
    //主轨轨入电压,     19
    //主轨轨入载频,     20
    //主轨轨入低频, 21
    //轨出1电压,  22
    //轨出1载频,  23
    //轨出1低频,  24
    //轨出2电压,  25
    //轨出2载频,  26
    //轨出2低频,  27
    //移频发送电缆侧电压,  28
    //移频接收电缆侧电压,  29
    //轨出1上边频, 30
    //轨出1下边频, 31
    //移频发送电缆侧电流,  32
    //道床电阻,   33
    //小轨轨入电压, 34
    //灯丝继电器电流,    35
    //灯丝继电器电压,    36
    //道岔表示电压, 37
    //半自动闭塞电压和电流, 38
    //异物继电器端电压,   39
    //站联电压,   40
    //道岔缺口,   41
    //环境模拟量,  42
    //区间监测,   43
    //移频发送电缆侧载频,  44
    //移频发送电缆侧低频,  45
    //移频接收电缆侧载频,  46
    //移频接收电缆侧低频,  47
    //受端电缆侧小轨电压,  48
    //受端电缆侧小轨载频,  49
    //受端电缆侧小轨低频,  50
    //小轨轨入载频, 51
    //小轨轨入低频  52
    public class Change10And18Type
    {
        //普通模拟量转换表
        private static Dictionary<int, int> analog10TypeTo18Type = new Dictionary<int, int> {
            {0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 5}, {5, 4}, {6, 6}, {7, 10}, {8, 8}, {9, 19},
            {10, 20}, {11, 21}, {12, 22}, {13, 23}, {14, 24}, {15, 25}, {16, 26}, {17, 27}, {18, 28}, {19, 29},
            {20, 30}, {21, 31}, {22, 32}, {23, 33}, {24, 34}, {25, 35}, {26, 36}, {27, 37}, {28, 38}, {29, 39},
            {30, 40}, {31, 41}, {32, 42}, {33, 43}, {34, 45}, {35, 15}, {36, 16}, {37, 11}, {38, 17}, {39, 61},
            {40, 18}, {41, 12}, {42, 57}, {43, 62}, {44, 48}, {45, 49}, {46, 50}, {47, 51}, {48, 56}, {49, 52},
            {50, 53}, {51, 54}, {52, 55}, {53, 47}, {59, 59}, {60, 60}, {255, 65535},
        };

        //曲线模拟量转换表
        private static Dictionary<int, int> curve10TypeTo18Type = new Dictionary<int, int>{
            {0, 16}, {1, 18}, {2, 17}, {3, 19}, {4, 32}, {5, 33}, {6, 34},
        };

        //18版模拟量类型转换互联互通码表
        private static Dictionary<int, int> analog18TypeToInterconnection = new Dictionary<int, int>{
            {0,16}, {1,17}, {2,18}, {3,32}, {4,33}, {5,34},
            {6,35}, {7,36}, {8,37}, {9,38}, {10,245}, {11,48},
            {12,49}, {13,50}, {14,51}, {15,64}, {16,65}, {17,66},
            {18,67}, {19,80}, {20,81}, {21,82}, {22,83}, {23,84},
            {24,85}, {25,88}, {26,89}, {27,90}, {28,91}, {29,92},
            {30,93}, {31,94}, {32,95}, {33,96}, {34,97}, {35,98},
            {36,99}, {37,100}, {38,101}, {39,102}, {40,103}, {41,104},
            {42,105}, {43,106}, {44,107}, {45,108}, {46,144}, {47,146},
            {48,109}, {49,110}, {50,111}, {51,118}, {52,114}, {53,115},
            {54,116}, {55,117}, {56,120}, {57,112}, {58,113}, {59,128},
            {60,129}, {61,145}, {62,147}, {63,148}, {64,149}, {65,150},
            {66,160}, {65535, 65535},
        };

        //18版曲线类型转换互联互通码表
        private static Dictionary<int, int> curve18TypeToInterconnection = new Dictionary<int, int>{
            {0,16}, {1,17}, {2,18}, {3,19}, {4,32},
            {5,33}, {6,34}, {7,35},
        };

        /// <summary>
        /// 将模拟量类型从18转为10版
        /// </summary>
        /// <param name="analogType18">18版模拟量类型</param>
        /// <returns>10版模拟量类型</returns>
        public static string Change18To10(string analogType18)
        {
            string analogType10 = "";
            analogType18 = analogType18.ToUpper();
            //检查是否为16进制字符串
            string regex = "^[A-Fa-f0-9]+$";
            int analogType10Num = -1;

            if (Regex.IsMatch(analogType18, regex))
            {
                int analogType18Num = Convert.ToInt32(analogType18, 16);

                foreach (KeyValuePair<int, int> kvp in analog10TypeTo18Type)
                {
                    if (analogType18Num == kvp.Value)
                    {
                        analogType10Num = kvp.Key;
                    }
                }
            }
            else
            {
                Debug.WriteLine(analogType18 + "不是16进制字符串");
            }
            if (analogType10Num != -1)
            {
                analogType10 = analogType10Num.ToString("x4");
            }
            else
            {
                analogType10 = "FFFF";
            }

            return analogType10;
        }

        /// <summary>
        /// 将模拟量类型从10转为18版
        /// </summary>
        /// <param name="analogType10">10版模拟量类型</param>
        /// <returns>18版模拟量类型</returns>
        public static string Change10To18(string analogType10)
        {
            string analogType18 = "";
            analogType10 = analogType10.ToUpper();
            //检查是否为16进制字符串
            string regex = "^[A-Fa-f0-9]+$";
            int analogType18Num = -1;

            if (Regex.IsMatch(analogType10, regex))
            {
                int analogType10Num = Convert.ToInt32(analogType10, 16);
                if (analog10TypeTo18Type.ContainsKey(analogType10Num))
                {
                    analogType18Num = analog10TypeTo18Type[analogType10Num];
                }
            }
            else
            {
                Debug.WriteLine(analogType10 + "不是16进制字符串");
            }
            if (analogType18Num != -1)
            {
                analogType18 = analogType18Num.ToString("x4");
            }
            else
            {
                analogType18 = "FFFF";
            }

            return analogType18;
        }

        /// <summary>
        /// 将曲线模拟量类型从10转为18版
        /// </summary>
        /// <param name="curveType10">10版曲线模拟量类型</param>
        /// <returns>18版模拟量类型</returns>
        public static string ChangeCurve10To18(string curveType10)
        {
            string curveType18 = "";
            curveType10 = curveType10.ToUpper();
            //检查是否为16进制字符串
            string regex = "^[A-Fa-f0-9]+$";
            int curveType18Num = -1;

            if (Regex.IsMatch(curveType10, regex))
            {
                int curveType10Num = Convert.ToInt32(curveType10, 16);
                if (curve10TypeTo18Type.ContainsKey(curveType10Num))
                {
                    curveType18Num = curve10TypeTo18Type[curveType10Num];
                }
            }
            else
            {
                Debug.WriteLine(curveType10 + "不是16进制字符串");
            }
            if (curveType18Num != -1)
            {
                curveType18 = curveType18Num.ToString("x4");
            }
            else
            {
                curveType18 = "FFFF";
            }

            return curveType18;
        }

        /// <summary>
        /// 将18版模拟量类型转为互联互通码
        /// </summary>
        /// <param name="analog18Type">18版模拟量类型</param>
        /// <returns>互联互通码</returns>
        public static int ChangeAnalog18TypeToInterconnect(int analog18Type)
        {
            int interconnectNum = -1;
            if (analog18TypeToInterconnection.ContainsKey(analog18Type))
                interconnectNum = analog18TypeToInterconnection[analog18Type];

            return interconnectNum;
        }

        /// <summary>
        /// 将18版模拟量类型转为互联互通码
        /// </summary>
        /// <param name="curve18Type">18版曲线类型</param>
        /// <returns>互联互通码</returns>
        public static int ChangeCurve18TypeToInterconnect(int curve18Type)
        {
            int interconnectNum = -1;
            if (curve18TypeToInterconnection.ContainsKey(curve18Type))
                interconnectNum = curve18TypeToInterconnection[curve18Type];

            return interconnectNum;
        }
    }
}
